package com.uzletiwebtech.mancala.ui.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.uzletiwebtech.mancala.User;

/**
 * Servlet implementation class GameServlet
 */
@WebServlet("/GameServlet")
public class GameServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String PLAYER = "player";
       
    public GameServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("GameServlet.doGet");

        getServletContext().getRequestDispatcher("/game.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("GameServlet.doPost");
		
		HttpSession session = request.getSession();
        User player = (User) session.getAttribute(PLAYER);
        
        try {
        	if (null != player && !player.getFullName().isEmpty()) {
        		System.out.println("Player logged in: " + player.getFullName());
        	}
			
        	getServletContext().getRequestDispatcher("/game.jsp").forward(request, response);
		} catch (Exception e) {
			System.err.println("Exception catched while processing GameServlet.doPost()");
			System.err.println(e.getCause());
			System.err.println(e);
			getServletContext().getRequestDispatcher("/game.jsp").forward(request, response);
		}
	}

}
